package com.hero.take.util;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

public class MapHelpper {
    public interface IMapEvent<T1, T2> {
        void action(T1 t1, T2 t2);
    }

    public static <T1, T2> void forEach(Map<T1, T2> map, Consumer<T2> consumer) {
        Set<T1> keys = map.keySet();
        Iterator<T1> keyIterator = keys.iterator();
        while (keyIterator.hasNext()) {
            T2 value = map.get(keyIterator.next());
            consumer.accept(value);
        }
    }

    public static <T1, T2> void forEach(Map<T1, T2> map, IMapEvent<T1, T2> consumer) {
        if (map == null) return;
        Set<T1> keys = map.keySet();
        Iterator<T1> keyIterator = keys.iterator();
        while (keyIterator.hasNext()) {
            T1 key = keyIterator.next();
            T2 value = map.get(key);
            consumer.action(key, value);
        }
    }

    /**
    public static <T> T getMapValue(Map map, String key, Class<T> clazz, T defaultValue) {
        if (StringUtils.isBlank(key) || map == null) return defaultValue;
        if (!map.containsKey(key)) return defaultValue;
        Object value = map.get(key);
        if (value == null) return defaultValue;
        T result = JsonHelpper.jsonToObject(JsonHelpper.objectToStr(value), clazz);
        return result == null ? defaultValue : result;
    }
     */
}
